package njupt.daoimp;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import njupt.dao.PingJiaDAO;

/**
 * @author leon(liangzou0318@gmail.com)
 * @date 2012-4-22
 * @filaname PingJiaimp.java
 */
public class PingJiaDAOimp implements PingJiaDAO {
	private Connection conn=null;
	private PreparedStatement ps=null ;
	private ResultSet rs=null;
     public PingJiaDAOimp(Connection conn){
			
			this.conn=conn;
			System.out.println("123pingjiaimp");
			}
     public int insertinfo(String pingjiainfo,String levelinfo,int userid,int goodsid) throws Exception {
 		int flag=0;
 		try{
 			 String sql="insert into product_pingjia(userid,goodsid,pingjia,is_pingjia,times,levelinfo) values(?,?,?,'1', sysdate,?)";
 			 System.out.println("sql update"+sql);
 			 ps=this.conn.prepareStatement(sql);
 			 this.ps.setInt(1, userid);
 			 this.ps.setInt(2, goodsid);
 			 this.ps.setString(3, pingjiainfo);
 			 this.ps.setString(4, levelinfo);
 			 //this.ps.setInt(3, orderid);
 			 this.ps.executeUpdate();
 			 flag=1; 
 		 }catch(Exception e){
 			 throw e;
 		 }
 		return flag;
     }
	/* (non-Javadoc)
	 * @see njupt.dao.PingJiaDAO#disply_pingjia(int)
	 */
	public List<String[]> disply_pingjia(int goodsid) throws Exception {
		 List<String[]> pingjia=new ArrayList<String[]>();
		 try{
			 String sql="select pingjia,times, levelinfo,userid from product_pingjia where goodsid=?";
			 ps=this.conn.prepareStatement(sql);
			 ps.setInt(1, goodsid);
			 rs=ps.executeQuery();
			// ResultSetMetaData rsmd=rs.getMetaData();
			   
			 while(rs.next()){	
				 String[] str=new String[4];
			     	 for(int i=0;i<4;i++){
			     		 str[i]=rs.getString(i+1);
			     	 }
				 pingjia.add(str);
			 }
		 }catch(Exception e){
			 throw e;
		 }
		return pingjia;
	}
	/* (non-Javadoc)
	 * @see njupt.dao.PingJiaDAO#createTousu(java.lang.String, int, int, java.lang.String)
	 */
	public int createTousu(String tousuinfo, int userid, int orderid,
			int tousuStyle) throws Exception {
		System.out.println("[][]createTousu[");
		           int flag=0;
		           try{
		        	  String sql="insert into tousu values(?,?,sysdate,?,?,0,null,null)";  //插入订单
		        	  
		        	  ps=this.conn.prepareStatement(sql);
		        	  ps.setString(1,tousuinfo);
		        	  ps.setInt(2, tousuStyle);
		        	  ps.setInt(3, orderid);
		        	  ps.setInt(4, userid);
		        	  
		        	  ps.executeUpdate();
		        	  System.out.println("[][][【】【createTousu】");
		        	  flag=1;
		           }catch(Exception e){		        	   
		           }
		return flag;
	}
	/* (non-Javadoc)
	 * @see njupt.dao.PingJiaDAO#createTousu(java.lang.String, int, int, java.lang.String)
	 */
	/* Overriding methods
	 */
	public List<String[]> select(int type) throws Exception {
		 List<String[]> tousuForward=new ArrayList<String[]>();
		 try{
			 String sql="select biaoshi,tousustyle,times,orderid,userid,tousuhander,tousuinfo from tousu where biaoshi=?";
			 ps=this.conn.prepareStatement(sql);
			 System.out.println("Type:   "+type);
			 ps.setInt(1, type);
			 rs=ps.executeQuery();
			// ResultSetMetaData rsmd=rs.getMetaData();			  
			 while(rs.next()){	
				 String[] str=new String[7];
			     	 for(int i=0;i<6;i++){
			     		 str[i]=rs.getString(i+1);
			     		 //System.out.println("orderid");
			     	 }
			     	System.out.println("tousuForward");
			     	tousuForward.add(str);
			 }
		 }catch(Exception e){
			 throw e;
		 }
		return tousuForward;
	}
	/* Overriding methods
	 */
	public int updateTousuStatus(int orderid,int status) throws Exception {
		String sql="update tousu set biaoshi=? where orderid=?";
		int flag=0;
		try{
			ps=this.conn.prepareStatement(sql);
			ps.setInt(1, status);
			ps.setInt(2, orderid);
			ps.executeUpdate();
			flag=1;
		}catch(Exception e){
			throw e;
		}
		return flag;
	}
	/* Overriding methods
	 */
	public List<String> selectTousuinfoToHander(int orderid) throws Exception {
		List<String> selectTousuinfoToHander=new ArrayList<String>();
		String sql="select tousuinfo from tousu where orderid=?";
		try{
			ps=this.conn.prepareStatement(sql);
			ps.setInt(1, orderid);
			rs=ps.executeQuery();
			while(rs.next()){
				String str = null;
		     	 for(int i=0;i<1;i++){
		     		 str=rs.getString(i+1);
		     	 }
		     	selectTousuinfoToHander.add(str); 
		     	selectTousuinfoToHander.add(String.valueOf(orderid));
			}
		}catch(Exception e){
			throw e;}
		return selectTousuinfoToHander;
	}
	/* Overriding methods
	 */
	public int insertTousuChuli(int orderid,String chuliinfo) throws Exception {
		int flag=0;
		try{
		String sql="update tousu set chuliinfo=? where orderid=?";
		 ps=this.conn.prepareStatement(sql);
		 ps.setString(1,chuliinfo);
		 ps.setInt(2, orderid);
		 ps.executeUpdate();
		 flag=1;
		}catch(Exception e){
			 throw e;
		 }
		
		return flag;
	}
	/* Overriding methods
	 */
	public List<String> selectTousuByCustmer(int orderid) throws Exception {
		List<String> selectTousuByCustmer=new ArrayList<String>();
		try{
			String sql="select tousuinfo,chuliinfo from tousu where orderid=?";
			ps=this.conn.prepareStatement(sql);
			ps.setInt(1, orderid);
			
			rs=ps.executeQuery();	
			System.out.println("List{12}"+orderid);
			while(rs.next()){
				selectTousuByCustmer.add(rs.getString(1));
				selectTousuByCustmer.add(rs.getString(2));
				System.out.println("rs.getString(2)   "+rs.getString(2));
			}
			System.out.println("List{122}");
		}catch(Exception e){throw e;}
		return selectTousuByCustmer;
	}
	/* Overriding methods
	 */
	public List<Integer> selectOrderidForCustmerShowTousu(int userid) throws Exception {
		List<Integer> selectOrderidForCustmerShowTousu=new ArrayList<Integer>();
		try{
			int count=0;
			String sql="select orderid from tousu where userid=? and biaoshi=3";
			ps=this.conn.prepareStatement(sql);
			ps.setInt(1, userid);
			rs=ps.executeQuery();
			while(rs.next()){
				count++;
				selectOrderidForCustmerShowTousu.add(rs.getInt(1));				
			}
			selectOrderidForCustmerShowTousu.add(count);
		}catch(Exception e){throw e;}
		return selectOrderidForCustmerShowTousu;
	}
	
     }
	/*public int insertinfo(String pingjiainfo,String levelinfo,int orderid) throws Exception {
		int flag=0;
		try{
			 String sql="update ordermsg set pingjiainfo=?,levelinfo=?,ispingjia='1' where orderid=?";
			 System.out.println("sql update"+sql);
			 ps=this.conn.prepareStatement(sql);
			 this.ps.setString(1, pingjiainfo);
			 this.ps.setString(2, levelinfo);
			 this.ps.setInt(3, orderid);
			 this.ps.executeUpdate();
			 flag=1; 
		 }catch(Exception e){
			 throw e;
		 }
		return flag;
	}*/


